<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-avator">
        <img src="../assets/logo.png" alt="">
      </div>

      <el-form ref="ruleForm" :model="form" :rules="rules" class="form-box" label-width="0">
        <el-form-item prop="username">
          <el-input 
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            type="password"
            prefix-icon="el-icon-unlock">
          </el-input>
        </el-form-item>
        <el-form-item class="btn-position">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          this.$message({
            message: '错了哦, 请填写完整信息',
            type: 'error'
          })
          return false
        }

        const {data} = await this.$http.post('/login', this.form)

        if(data.meta.status !== 200) {
          this.$message({
            message: data.meta.msg,
            type: 'warning'
          })
          return false
        }

        this.$message({
          message: data.meta.msg,
          type: 'success'
        })
        
        sessionStorage.setItem('token', data.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  position: relative;
  background-color: #2b4b6b;
  .login-box {
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: #fff;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .login-avator {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50px);
      background-color: #fff;
      width: 100px;
      height: 100px;
      border-radius: 50%;
      cursor: pointer;
      overflow: hidden;
      border: 1px solid #eee;
      padding: 5px;
      box-shadow: 0 0 10px #ddd;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }

  .form-box{
    position: absolute;
    bottom: 20px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btn-position{
    display: flex;
    justify-content: flex-end;
  }
}
</style>